/* software_i2c.h */
#ifndef __SOFTWARE_I2C_H__
#define __SOFTWARE_I2C_H__

#include "main.h"  // 替换为你的HAL库头文件

typedef struct {
    GPIO_TypeDef* scl_port;  // SCL端口
    uint16_t scl_pin;        // SCL引脚
    GPIO_TypeDef* sda_port;  // SDA端口
    uint16_t sda_pin;        // SDA引脚
    uint32_t speed;          // 速率 (Hz)
    uint32_t delay_us;          // 内部使用的延时值
} SoftwareI2C_HandleTypeDef;

// 初始化软件I2C
void SoftI2C_Init(SoftwareI2C_HandleTypeDef *hi2c);

// 基本操作函数
void SoftI2C_Start(SoftwareI2C_HandleTypeDef *hi2c);
void SoftI2C_Stop(SoftwareI2C_HandleTypeDef *hi2c);
uint8_t SoftI2C_WriteByte(SoftwareI2C_HandleTypeDef *hi2c, uint8_t byte);
uint8_t SoftI2C_ReadByte(SoftwareI2C_HandleTypeDef *hi2c, uint8_t ack);
void SoftI2C_SendNAck(SoftwareI2C_HandleTypeDef* hi2c);
void SoftI2C_SendAck(SoftwareI2C_HandleTypeDef* hi2c);
uint8_t SoftI2C_WaitAck(SoftwareI2C_HandleTypeDef *hi2c, uint32_t timeout);

// 高级功能函数
HAL_StatusTypeDef SoftI2C_Mem_Write(
    SoftwareI2C_HandleTypeDef *hi2c,
    uint16_t dev_addr,
    uint16_t mem_addr,
    uint16_t mem_addr_size,
    uint8_t *data,
    uint16_t size
);

HAL_StatusTypeDef SoftI2C_Mem_Read(
    SoftwareI2C_HandleTypeDef *hi2c,
    uint16_t dev_addr,
    uint16_t mem_addr,
    uint16_t mem_addr_size,
    uint8_t *data,
    uint16_t size
);

#endif